using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sec_scan漏洞扫描系统
{
    public partial class Sec_Scan_Addloop : Form
    {
        public Sec_Scan_Addloop()
        {
            InitializeComponent();
        }
        public string code;//验证码
        public int oldX, oldY;
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            ReferCode();
        }
        public void ReferCode()
        {
            code = Sec_Config.Sec_CodeInfo.GenerateCheckCode();
            Bitmap image = Sec_Config.Sec_CodeInfo.CreateCheckCodeImage(code, 64, 30);//长高
            pictureBox2.Image = image;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //添加漏洞
            if (string.IsNullOrEmpty(watermarkTextBox1.Text)||string.IsNullOrEmpty(watermarkTextBox2.Text)||string.IsNullOrEmpty(watermarkTextBox3.Text))
            {
                MessageBox.Show("漏洞数据或有效载荷及关键特征不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(comboBox1.Text) || string.IsNullOrEmpty(comboBox2.Text) || string.IsNullOrEmpty(comboBox3.Text))
            {
                MessageBox.Show("选择不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(watermarkTextBox8.Text))
            {
                MessageBox.Show("验证码不能为空！");
                return;
            }
            if (watermarkTextBox8.Text.Equals(code))
            {
                //验证码正确的情况下 执行添加数据
                AddLoopInfo();
            }
            else
            {
                MessageBox.Show("验证码错误，请重试！");
                ReferCode();
            }
        }
        /// <summary>
        /// 添加漏洞数据
        /// </summary>
        public void AddLoopInfo()
        {
            //针对部分字符串进行加密
            string payload = Sec_Config.Sec_Base64Code.Tobase64(watermarkTextBox2.Text);//有效载荷
            string key = Sec_Config.Sec_Base64Code.Tobase64(watermarkTextBox3.Text);//关键特征

            string key_1 = Sec_Api.Sec_DesThreDesApi.DesKeyInfo(Sec_Api.Sec_DesThreDesApi.D1);//密文一
            string key_2 = Sec_Api.Sec_DesThreDesApi.DeskeyInfoOne(Sec_Api.Sec_DesThreDesApi.D1);//密文二
            string key_3 = Sec_Api.Sec_DesThreDesApi.DeskeyInfoTwo(Sec_Api.Sec_DesThreDesApi.D1);//密文三

            string payloads = Sec_Config.Sec_DesThreDes.Encrypt(payload, key_1, key_2, key_3);//对有效载荷进行加密
            string keys = Sec_Config.Sec_DesThreDes.Encrypt(key, key_1, key_2, key_3);//对关键特征进行加密

            string path = Sec_Api.Sec_AllAPI.Sqlclient(Sec_Api.Sec_LoopScan_Service_Api.P1);//链接字符串
            //w6 生成编号
            SQLiteConnection connection = new SQLiteConnection(path);
            try
            {
                connection.Open();
                if(connection.State == ConnectionState.Open)
                {
                    //数据库能打开的情况下，执行创建漏洞编号
                    Sec_Config.Sec_LoopNumber.NumberInfo(Sec_Api.Sec_LoopNumberApi.M1);
                    string num = Sec_Api.Sec_LoopNumberApi.LoopNum;
                    //插入到数据库
                    string sql = "INSERT INTO Sec_LoopDetails (sec_loopname, sec_looppayload, sec_loopkeyword, sec_loopaddr, sec_looptype, sec_looplevel, sec_loopnumber, sec_cnvd, sec_cnnvd, sec_cve, sec_revise, sec_accuracy, sec_addtime) VALUES('"+watermarkTextBox1.Text+"', '"+ payloads + "', '"+keys+"', '"+watermarkTextBox4.Text+"', '"+comboBox1.Text+"', '"+comboBox2.Text+"', '"+num+"', '"+watermarkTextBox5.Text+"', '"+watermarkTextBox6.Text+"', '"+watermarkTextBox7.Text+"', '"+richTextBox1.Text+"', '"+comboBox3.Text+"', '"+DateTime.Now+"')";
                    SQLiteCommand command = new SQLiteCommand(sql, connection);
                    command.ExecuteReader();
                    MessageBox.Show("漏洞数据添加成功！已进行加密处理！");
                    this.Close();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Addloop_Load(object sender, EventArgs e)
        {
            ReferCode();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            //判断是否为鼠标左键
            if (e.Button == MouseButtons.Left)
            {
                //获取鼠标左键按下时的位置
                this.oldX = e.Location.X;
                this.oldY = e.Location.Y;
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //计算鼠标移动距离
                this.Left += e.Location.X - this.oldX;
                this.Top += e.Location.Y - this.oldY;
            }
        }
    }
}
